package com.example.waibao.controller;


import com.example.waibao.Component.UserHelper;
import com.example.waibao.config.RankListComponent;
import com.example.waibao.entity.ResponseResult;
import com.example.waibao.entity.User;
import com.example.waibao.entity.rank.RankDO;
import com.example.waibao.entity.rank.RankDO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RankController {
    @Autowired
    private RankListComponent rankListComponent;


    @Autowired
    private UserHelper userHelper;

    @RequestMapping(value = "/rank_update", produces = "text/event-stream;charset=UTF-8")
    public String push() {
        try {
            Thread.sleep(5000);
            //第三方数据源调用
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "data:排行榜数据更新" +  "\n\n";
    }

    @GetMapping(path = "/topn")
    public ResponseResult showTopN(@RequestParam(name = "n", defaultValue = "10") int n, int speciality_id, int type) {
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        List<RankDO> rank = rankListComponent.getTopNRanks(n,school_id,speciality_id,type);

        //将分数取整
        for(RankDO rankDO : rank){

            rankDO.setStudent_score(new Float(Math.floor(rankDO.getStudent_score())));

        }

        return new ResponseResult(200,"学生排行榜获取成功",rank);

    }

//    @GetMapping(path = "/topn")
//    public ResponseResult showTopN(@RequestParam(name = "n", defaultValue = "10") int n) {
//        User user = userHelper.getCurrentUser();
//        int school_id = user.getSchool_id();
//        List<RankDO> rank = rankListComponent.getTopNRanks(n,school_id);
//
//        //将分数取整
//        for(RankDO rankDO : rank){
//          rankDO.setStudent_score(new Float(Math.floor(rankDO.getStudent_score())))  ;
//        }
//        return new ResponseResult(200,"学生排行榜获取成功",rank);
//    }


    @GetMapping(path = "/get_speciality_rank")
    public ResponseResult get_speciality_rank(@RequestParam(name = "n", defaultValue = "10")int n) {

        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        List<RankDO2> rank = rankListComponent.get_speciality_rank(n,school_id);

        //将分数取整
        for(RankDO2 rankDO2 : rank){
            rankDO2.setSpeciality_score(new Float(Math.floor(rankDO2.getSpeciality_score())));  ;
        }
        return new ResponseResult(200,"学校专业排行榜获取成功",rank);
    }


    @GetMapping(path = "/update")
    public RankDO updateScore(String student_name, float score) {
        return rankListComponent.updateRank(student_name, score);
    }

    @GetMapping(path = "/rank")
    public RankDO queryRank(String student_name) {
        return rankListComponent.getRank(student_name);
    }

    @GetMapping(path = "/around")
    public ResponseResult around(String student_name, int n) {

        return  new ResponseResult(200,"查询排行榜成功" ,rankListComponent.getRankAroundUser(student_name, n));

    }
}
